package com.udi.project.business.local.di.service.Impl;


import com.udi.common.exception.base.BaseException;
import com.udi.framework.web.domain.AjaxResult;
import com.udi.project.business.base.domain.BaseCorpVo;
import com.udi.project.business.local.di.domain.DtDi;
import com.udi.project.business.upload.diup.domain.Product;
import com.udi.project.business.pojo.damain.*;
import com.udi.project.business.local.di.mapper.DiMangeMapper;
import com.udi.project.business.local.di.service.DiMangeService;
import com.udi.project.business.local.di.thread.Recycle;
import com.udi.project.business.pojo.damain.Package;
import com.udi.project.business.utils.redis.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author Admin
 */
@Service
@Slf4j
public class DiMangeServiceImpl implements DiMangeService {

    @Autowired
    private DiMangeMapper diMangeMapper;
    @Autowired
    private RedisUtils redisUtils;
    
    /**
     *
     * @param DtDi 列表查询 
     [DtDi]* @return java.util.List<com.udi.project.business.local.di.domain.DtDi>
     * @author yuxin
     * @creed: Talk is cheap,show me the code
     * @date 2021/10/18 17:30
     */
    @Override
    public List<DtDi> selectDi(DtDi DtDi) {
        return diMangeMapper.selectDi(DtDi);
    }

    /**
     *
     * @param DtDi 列表查询 已导出使用的
    [DtDi]* @return java.util.List<com.udi.project.business.local.di.domain.DtDi>
     * @author yuxin
     * @creed: Talk is cheap,show me the code
     * @date 2021/10/18 17:30
     */
    @Override
    public List<DtDi> selectDils(DtDi DtDi) {
        return diMangeMapper.selectDils(DtDi);
    }

    /**
     * @param di 根据di表id查询一行信息
     */
    @Override
    public DtDi selectDiById(Integer di) {
        return diMangeMapper.selectDiById(di);
    }
    /**
     * @param di 根据di表id查询一行信息
     */
    @Override
    public DtDi selectDilsById(Integer di) {
        return diMangeMapper.selectDilsById(di);
    }

    @Override
    public List<dtPkg> selectPkg() {
        return diMangeMapper.selectPkg();
    }


    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    @Override
    public int delectDi(String code) throws Exception {
        String[] strs = code.split(",");
        int result = diMangeMapper.delectDi(strs);
        for(String di:strs){
            redisUtils.lSet("model",di);
        }
        //启动回收子线程
        Recycle recycle = new Recycle();
        recycle.start();
        if(result != 0 ){
            return result;
        }else {
            log.info("影响条数为----------》"+result);
            throw new Exception("删除失败");
        }

    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    @Override
    public int insertDi(List<DtDi> udiMange) throws Exception {
        int result = diMangeMapper.insertDi(udiMange);
        if(result != 0 ){
            return result;
        }else {
            log.info("影响条数为----------》"+result);
            throw new Exception("插入失败");
        }
    }

    /**
     * @param statu
     * @Description:更新DI状态和使用途径
     * @return: int
     * @Date: 2021/8/9 11:29
     **/
    @Override
    public int updateStatu(List<DtDi> di) {
        return diMangeMapper.updateStatu(di);
    }


    /**
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    @Override
    public int updateDi(List<DtDi> udiMange) throws Exception {
        int result = diMangeMapper.updateDi(udiMange);
        if(result != 0 ){
            return result;
        }else {
            log.info("影响条数为----------》"+result);
            throw new Exception("更新失败");
        }
    }
    */

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    @Override
    public AjaxResult insertInformation(List<Product> identity) {
        int ove =  diMangeMapper.insertInformation(identity);
        if(ove==0){
            throw new BaseException("持久化失败");
        }
        return AjaxResult.success("操作成功");
    }






    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    @Override
    public AjaxResult updateInformation(List<Product> identity) {
        int ove =  diMangeMapper.updateInformation(identity);
        if(ove==0){
            throw new BaseException("更改失败");
        }
        return AjaxResult.success("操作成功");
    }





    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    @Override
    public AjaxResult  delectInformation(String di_id) {
        int ove =  diMangeMapper.delectInformation(di_id);
        if(ove==0){
            throw new BaseException("删除失败");
        }
        return AjaxResult.success("操作成功");
    }



    @Override
    public List<Product> selectInformation(String di) {
        return diMangeMapper.selectInformation(di);
    }




    @Override
    public List<Package> selectpkgDi(List<String> id) {
        return diMangeMapper.selectpkgDi(id);
    }

    @Override
    public List<Package> selectpkg(String id) {
        return diMangeMapper.selectpkg(id);
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    @Override
    public String insertPkg(List<Package> packag) {
        int ims =  diMangeMapper.insertPkg(packag);
        if(ims == 0){
            log.info("包装标识信息插入失败");
            return "插入失败";
        }
        return "操作成功";
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    @Override
    public String updatePkg(List<Package> packag) {
        int ims =  diMangeMapper.updatePkg(packag);
        if(ims == 0){
            log.info("包装标识信息更新失败");
            return "更新失败";
        }
        return "操作成功";
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    @Override
    public String deletePkg(List<Package> id) {
        int ims =  diMangeMapper.deletePkg(id);
        if(ims == 0){
            log.info("包装标识信息更新失败");
            return "更新失败";
        }
        return "操作成功";
    }

    @Override
    public List<Storage> selectStorageDi(List<String> id) {
        return diMangeMapper.selectStorageDi(id);
    }

    @Override
    public List<Storage> selectStorage(String id) {
        return diMangeMapper.selectStorage(id);
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    @Override
    public String insertStorage(List<Storage> storage) {
        int ims =  diMangeMapper.insertStorage(storage);
        if(ims == 0){
            log.info("存储或操作信息插入失败");
            return "插入失败";
        }
        return "操作成功";
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    @Override
    public String updateStorage(List<Storage> storage) {
        int ims =  diMangeMapper.updateStorage(storage);
        if(ims == 0){
            log.info("存储或操作信息更新失败");
            return "更新失败";
        }
        return "操作成功";
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    @Override
    public String deleteStorage(List<Storage> id) {
        int ims =  diMangeMapper.deleteStorage(id);
        if(ims == 0){
            log.info("存储或操作信息删除失败");
            return "删除失败";
        }
        return "操作成功";
    }

    @Override
    public List<Clinical> selectClinicalDi(List<String> id) {
        return diMangeMapper.selectClinicalDi(id);
    }

    @Override
    public List<Clinical> selectClinical(String id) {
        return diMangeMapper.selectClinical(id);
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    @Override
    public String insertClinical(List<Clinical> clinical) {
        int ims =  diMangeMapper.insertClinical(clinical);
        if(ims == 0){
            log.info("临床尺寸信息插入失败");
            return "插入失败";
        }
        return "操作成功";
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    @Override
    public String updateClinical(List<Clinical> clinical) {
        int ims =  diMangeMapper.updateClinical(clinical);
        if(ims == 0){
            log.info("临床尺寸信息更新失败");
            return "更新失败";
        }
        return "操作成功";
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    @Override
    public String deleteClinical(List<Clinical> id) {
        int ims =  diMangeMapper.deleteClinical(id);
        if(ims == 0){
            log.info("临床尺寸信息删除失败");
            return "删除失败";
        }
        return "操作成功";
    }

    @Override
    public List<BaseCorpVo> getdept(String id) {
        return diMangeMapper.getdept(id);
    }


}
